<template>
<el-col :xl="8" :lg="12" :md="24" :sm="24" :xs="24">
  <el-form :model="paForm" :rules="rules" ref="paForm" label-width="130px" size="small">
    <el-form-item label="appid" prop="appid">
      <el-input v-model="paForm.appid"></el-input>
    </el-form-item>
    <el-form-item label="微信公众号APPID" prop="wechatapp_id">
      <el-input v-model="paForm.wechatapp_id"></el-input>
    </el-form-item>
    <el-form-item label="微信公众号secret" prop="secret">
      <el-input v-model="paForm.secret"></el-input>
    </el-form-item>
    <el-form-item label="软件生成的应用私钥" prop="private_key">
      <el-input type="textarea" :rows="5" v-model="paForm.private_key"></el-input>
    </el-form-item>
    <el-form-item label="RSA应用公钥" prop="public_key">
      <el-input type="textarea" :rows="5" v-model="paForm.public_key"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" class="fr mt20" @click="submit">确认保存</el-button>
    </el-form-item>
  </el-form>
</el-col>
</template>

<script>
import { paInit, paAdd } from '@/api/setting'

export default {
  data() {
    const validateIsUndefined = (rule, value, callback) => {
      if (value === '' || value === undefined || value === null) {
        callback(new Error('不能为空哟~'))
      } else {
        callback()
      }
    }
    return {
      paForm: {
        appid: '',
        wechatapp_id: '',
        secret: '',
        private_key: '',
        public_key: ''
      },
      rules: {
        appid: [{ required: true, trigger: 'blur', validator: validateIsUndefined }],
        wechatapp_id: [{ required: true, trigger: 'blur', validator: validateIsUndefined }],
        secret: [{ required: true, trigger: 'blur', validator: validateIsUndefined }],
        private_key: [{ required: true, trigger: 'blur', validator: validateIsUndefined }],
        public_key: [{ required: true, trigger: 'blur', validator: validateIsUndefined }]
      }
    }
  },
  methods: {
    submit() {
      this.$refs['paForm'].validate((valid) => {
        if (valid) {
          paAdd(this.paForm).then(response => {
            if (response.data.errorCode === 0) {
              this.$message.success(response.data.errorMsg)
            } else {
              this.$message.error(response.data.errorMsg)
            }
          })
        } else {
          this.$message.error('表单格式有误哟~')
        }
      })
    }
  },
  created() {
    paInit().then(response => {
      this.paForm.appid = response.data.result.app_id
      this.paForm.wechatapp_id = response.data.result.wechatapp_id
      this.paForm.secret = response.data.result.secret
      this.paForm.private_key = response.data.result.private_key
      this.paForm.public_key = response.data.result.public_key
    })
  }
}
</script>